import { useI18n } from '/@/hooks/web/useI18n';

/**
 * 根据权限构建菜单Tree数据
 * @param permission
 * @param menuData
 * @param route
 * @returns
 */
export function getMenuTreePermission(permission, menuData, route) {
  const { t } = useI18n();
  let index = 0;
  for (let i = 0; i < route.length; i++) {
    let flag = false;
    if (route[i].meta && route[i].meta.priv) {
      for (let j = 0, len = permission.length; j < len; j++) {
        flag = route[i].meta.priv.includes(permission[j].id);
        if (flag) {
          const items = permission[j].items;
          if (items) {
            for (let k = 0; k < items.length; k++) {
              items[k].title = items[k].name;
              items[k].key = items[k].id;
              items[k].selectable = items[k].selectable;
              items[k].class = 'tree_float';
            }
          }
          menuData.push({
            key: permission[j].id,
            title: t(route[i].meta.title),
            disabled: permission[j].disabled,
            children: items,
            selectable: false,
            class: 'tree_clear',
          });
          index++;
          if (route[i].children && route[i].children.length > 0) {
            getMenuTreePermission(permission, menuData[index - 1].children, route[i].children);
          }
        }
      }
    }
  }
  return menuData;
}
